import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CollegeClubManagementSystem {
    public static void main(String[] args) {
        List<Club> clubs = new ArrayList<>();
        Scanner scanner = new Scanner(System.in);

        while (true) {
            System.out.println("1. 添加社团");
            System.out.println("2. 将部门添加到社团");
            System.out.println("3. 将学生添加到社团");
            System.out.println("4. 将学生从社团中移除");
            System.out.println("5. 将活动添加到社团");
            System.out.println("6. 查询社团");
            System.out.println("7. 将所有社团成员导出到文件");
            System.out.println("8. 将所有社团活动导出到文件");
            System.out.println("9. 将所有社团部门导出到文件");
            System.out.println("10. 退出");

            int choice = scanner.nextInt();
            scanner.nextLine();

            switch (choice) {
                case 1:
                    System.out.println("输入社团名称:");
                    String clubName = scanner.nextLine();
                    Club club = new Club(clubName);
                    clubs.add(club);
                    break;
                case 2:
                    System.out.println("输入社团名称:");
                    clubName = scanner.nextLine();
                    Club selectedClub = findClub(clubs, clubName);
                    if (selectedClub!= null) {
                        System.out.println("输入部门名称:");
                        String departmentName = scanner.nextLine();
                        selectedClub.addDepartment(departmentName);
                    } else {
                        System.out.println("社团未找到.");
                    }
                    break;
                case 3:
                    System.out.println("输入社团名称:");
                    clubName = scanner.nextLine();
                    selectedClub = findClub(clubs, clubName);
                    if (selectedClub!= null) {
                        System.out.println("输入学生姓名:");
                        String studentName = scanner.nextLine();
                        System.out.println("输入学生专业:");
                        String studentMajor = scanner.nextLine();
                        Student student = new Student(studentName, studentMajor);
                        selectedClub.addMember(student);
                    } else {
                        System.out.println("社团未找到.");
                    }
                    break;
                case 4:
                    System.out.println("输入社团名称:");
                    clubName = scanner.nextLine();
                    selectedClub = findClub(clubs, clubName);
                    if (selectedClub!= null) {
                        System.out.println("输入要移除的学生的名字:");
                        String studentName = scanner.nextLine();
                        List<Student> members = selectedClub.getMembers();
                        for (Student s : members) {
                            if (s.getName().equals(studentName)) {
                                selectedClub.removeMember(s);
                                break;
                            }
                        }
                    } else {
                        System.out.println("社团未找到.");
                    }
                    break;
                case 5:
                    System.out.println("输入社团名称:");
                    clubName = scanner.nextLine();
                    selectedClub = findClub(clubs, clubName);
                    if (selectedClub!= null) {
                        System.out.println("输入活动名称:");
                        String activityName = scanner.nextLine();
                        selectedClub.addActivity(activityName);
                    } else {
                        System.out.println("社团未找到.");
                    }
                    break;
                case 6:
                    for (Club c : clubs) {
                        System.out.println("社团: " + c.getName());
                        System.out.println("部门: " + c.getDepartments());
                        System.out.println("成员:");
                        for (Student s : c.getMembers()) {
                            System.out.println(s.getName() + " - " + s.getMajor());
                        }
                        System.out.println("活动: " + c.getActivities());
                    }
                    break;
                case 7:
                    try {
                        FileWriter writer = new FileWriter("club_members.txt");
                        for (Club c : clubs) {
                            for (Student s : c.getMembers()) {
                                writer.write(c.getName() + " - " + s.getName() + " - " + s.getMajor() + "\n");
                            }
                        }
                        writer.close();
                        System.out.println("所有社团成员导出到 club_members.txt.");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
                case 8:
                    try {
                        FileWriter writer = new FileWriter("club_activities.txt");
                        for (Club c : clubs) {
                            writer.write(c.getName() + " - " + c.getActivities()+"\n");
                        }
                        writer.close();
                        System.out.println("所有社团活动导出到 club_activities.txt.");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
                case 9:
                    try {
                        FileWriter writer = new FileWriter("club_departments.txt");
                        for (Club c : clubs) {
                            writer.write(c.getName() + " - " + c.getDepartments() + "\n");
                        }
                        writer.close();
                        System.out.println("所有社团部门导出到 club_departments.txt.");
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
                case 10:
                    scanner.close();
                    System.exit(0);
                default:
                    System.out.println("无效选择.");
            }
        }
    }

    private static Club findClub(List<Club> clubs, String name) {
        for (Club club : clubs) {
            if (club.getName().equals(name)) {
                return club;
            }
        }
        return null;
    }
}
